package com.cakebackoffice;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cakebackoffice.Model.Store;

import java.util.Stack;


public class AdminStoreDetailFragment extends Fragment {

    private LinearLayout mDetailLayout;
    private LinearLayout mWelcomeLayout;
    private Store selectedStore;


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDetailLayout = (LinearLayout) inflater.inflate(R.layout.admin_store_detail_fragment, container, false);
        mWelcomeLayout = (LinearLayout) inflater.inflate(R.layout.admin_store_detail_welcome_fragment, container, false);

        return mWelcomeLayout;
    }

    void setFragmentContentView(View view) {
        LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.FILL_PARENT);
        mWelcomeLayout.removeAllViews();
        mWelcomeLayout.addView(view, layoutParams);
    }

    public void displayStore(Store store) {
        selectedStore = store;
        setFragmentContentView(mDetailLayout);
        TextView tvStoreName = (TextView) getView().findViewById(R.id.store_textview_name);
        TextView tvEstimatedTime = (TextView) getView().findViewById(R.id.store_textview_estimatedtime);
        TextView tvStoreStreet = (TextView) getView().findViewById(R.id.store_textview_adressstreet);
        TextView tvStoreCity = (TextView) getView().findViewById(R.id.store_textview_adresscity);
        TextView tvStorePostalCode = (TextView) getView().findViewById(R.id.store_textview_adresspostalcode);
        TextView tvStoreCountry = (TextView) getView().findViewById(R.id.store_textview_adresscountry);
        LinearLayout adressLayout = (LinearLayout) getView().findViewById(R.id.store_layout_adress);
        adressLayout.setOnClickListener(onAdressViewClick);

        tvStoreName.setText(store.getName());
        tvStoreName.setOnClickListener(onNameTextViewClick);
        tvEstimatedTime.setText(Float.toString(store.getEstimatedServiceTime()));
        tvEstimatedTime.setOnClickListener(onTimeTextViewClick);

        Button buttonSelectStore = (Button) getView().findViewById(R.id.buttonChooseStore);
        buttonSelectStore.setOnClickListener(onButtonSelectStoreClick);


        Store localStore = Store.getLocalStore(getActivity().getBaseContext());
        if (localStore != null && Store.getLocalStore(getActivity().getBaseContext()).getRemoteID() == selectedStore.getRemoteID()) {
            buttonSelectStore.setClickable(false);
            buttonSelectStore.setText("This is the local store");

        } else {
            buttonSelectStore.setClickable(true);
            buttonSelectStore.setText("Select this store");
        }


        try {
            Stack<String> stack = new Stack<String>();
            if (store.getPlace().getAddress().getLine1() != null)
                stack.push(store.getPlace().getAddress().getLine1());
            if (store.getPlace().getAddress().getLine2() != null)
                stack.push(store.getPlace().getAddress().getLine2());
            if (store.getPlace().getAddress().getLine3() != null)
                stack.push(store.getPlace().getAddress().getLine3());
            if (store.getPlace().getAddress().getLine4() != null)
                stack.push(store.getPlace().getAddress().getLine4());
            String address = "";
            if (!stack.isEmpty()) {
                address = stack.pop();
            }
            while (!stack.isEmpty()) {
                address = address + " " + stack.pop();
            }
            tvStoreStreet.setText(address);
            tvStoreCity.setText(store.getPlace().getAddress().getCity());
            tvStorePostalCode.setText(store.getPlace().getAddress().getPostalCode());
            tvStoreCountry.setText(store.getPlace().getAddress().getCountry());
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    // Les listenners pour modifier les propriétés !

    // Nom du magasin
    private View.OnClickListener onNameTextViewClick = new View.OnClickListener() {
        public void onClick(View v) {
            ((AdminStoreActivity) getActivity()).openDialogEditName();
        }
    };

    // Temps moyen de commande
    private View.OnClickListener onTimeTextViewClick = new View.OnClickListener() {
        public void onClick(View v) {
            ((AdminStoreActivity) getActivity()).openDialogEditTime();
        }
    };

    // Changement d'adresse
    private View.OnClickListener onAdressViewClick = new View.OnClickListener() {
        public void onClick(View v) {
            ((AdminStoreActivity) getActivity()).openDialogEditAdress();
        }
    };

    // Le listenner pour définir le store local
    // Changement d'adresse
    private View.OnClickListener onButtonSelectStoreClick = new View.OnClickListener() {
        public void onClick(View v) {
            ((AdminStoreActivity) getActivity()).setLocalStore();
        }
    };


}
